package ru.mirent;

import io.qameta.allure.attachment.DefaultAttachmentProcessor;
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
import io.qameta.allure.jsonunit.*;
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
import org.hamcrest.Description;

/**
 * Аналог класса {@link JsonPatchMatcher}, с измененным именем шаблона Freemarker в методе {@link #render(DifferenceListener)}
 * на кастомный.
 * @param <T>
 */
public class CustomJsonPatchMatcher<T> extends AbstractJsonPatchMatcher<AllureConfigurableJsonMatcher<T>>
        implements AllureConfigurableJsonMatcher<T> {
    private final Object expected;

    private CustomJsonPatchMatcher(Object expected) {
        this.expected = expected;
    }

    public static <T> AllureConfigurableJsonMatcher<T> jsonEquals(Object expected) {
        return new CustomJsonPatchMatcher<>(expected);
    }

    public boolean matches(Object actual) {
        super.withDifferenceListener(new JsonPatchListener());
        return super.matches(this.expected, actual);
    }

    public void describeTo(Description description) {
        description.appendText("has no difference");
    }

    public void describeMismatch(Object item, Description description) {
        description.appendText(super.getDifferences());
    }

    public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
    }

    protected void render(DifferenceListener listener) {
        JsonPatchListener jsonDiffListener = (JsonPatchListener)listener;
        DiffAttachment attachment = new DiffAttachment(jsonDiffListener.getDiffModel());
        (new DefaultAttachmentProcessor()).addAttachment(attachment, new FreemarkerAttachmentRenderer("custom-diff.ftl"));
    }
}
